package org.skycastle.megastructure

import com.jme.util.{NanoTimer, Timer}
import space.Space

/**
 * 
 *
 * @author Hans Haggstrom
 */

class Universe {

  val timer = new NanoTimer()

  private var spaces : List[Space] = Nil

  def getSpaces : List[Space] = spaces

  def update() {

    timer.update

    val timeSinceLastUpdate_s = timer.getTimePerFrame();

    spaces foreach { _.update( timeSinceLastUpdate_s ) }

  }

  def addSpace( space : Space ) {
    require( space != null && !spaces.contains( space )   )

    val oldUniverse: Universe = space.getUniverse
    if (oldUniverse != null) oldUniverse.removeSpace( space )

    space.setUniverse( this )

    spaces = space :: spaces
  }

  def removeSpace( space : Space ) {
    require( space != null  )

    space.setUniverse( null )

    spaces = spaces.remove( _ == space )
  }



}